package cn.action;

import cn.base.BaseAction;
import cn.domain.*;
import cn.utils.HQLHelper;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/5 0005.
 */
@Controller
@Scope("prototype")
public class TopicAction extends BaseAction<Topic>{
    /**
     * <select name="viewType">
     <option value="0">全部主题</option>
     <option value="1">全部精华贴</option>
     <!--
     <option value="2">当天的主题</option>
     <option value="3">本周的主题</option>
     <option value="4">本月的主题</option>
     -->
     </select>
     */
    private int viewType;
    /**
     * <select name="orderBy">
     <option value="0">默认排序（按最后更新时间排序，但所有置顶帖都在前面）</option>
     <option value="1">按最后更新时间排序</option>
     <option value="2">按主题发表时间排序</option>
     <option value="3">按回复数量排序</option>
     </select>
     */
    private int orDerBy;

    /**
     * <select name="reverse">
     <option value="true">降序</option>
     <option value="false">升序</option>
     </select>
     */
    private boolean reverse;


    public int getViewType() {
        return viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public int getOrDerBy() {
        return orDerBy;
    }

    public void setOrDerBy(int orDerBy) {
        this.orDerBy = orDerBy;
    }

    public boolean isReverse() {
        return reverse;
    }

    public void setReverse(boolean reverse) {
        this.reverse = reverse;
    }

    private Long forumId;


    public Long getForumId() {
        return forumId;
    }

    public void setForumId(Long forumId) {

        this.forumId = forumId;
    }

    /**
     * 查询所有板块
     * @return
     */
    public String findAll(){

//        List<Forum> list  =  forumService.findAll();
//        getValueStack().set("list",list);
        HQLHelper hqlHelper=new HQLHelper(Forum.class);
        PageBean pageBean=forumService.getPageBean(hqlHelper,getCurrentPage());
        getValueStack().set("list",pageBean.getRecordList());
        getValueStack().push(pageBean);
        return "toList";
    }

    /**
     * 查询某个板块的所有主题
     * @return
     */
    public String Show(){
        Forum forum=forumService.getById(model.getId());
//        List<Topic> list=topicService.Show(model.getId());
//        getValueStack().set("show",list);
        getValueStack().set("forum",forum);
        HQLHelper hqlHelper=new HQLHelper(Topic.class);
        if(viewType==1){//全部精华贴
            hqlHelper.addWhere("type=?",1);
        }

        if(orDerBy==0){//默认排序（按最后更新时间排序，但所有置顶帖都在前面）
            //from Topic where ForumId=? ORDER BY (case type when  2  then 2 else 1 end) desc , postTime Desc";
            hqlHelper.orDerBy("(case type when 2 then 2 else 1 end)",false);
            hqlHelper.orDerBy("postTime",false);
        }
        else if(orDerBy==1){//按最后更新时间排序
            hqlHelper.orDerBy("lastUpTime",reverse);
        }
        else if(orDerBy==2){//按主题发表时间排序
            hqlHelper.orDerBy("postTime",reverse);
        }else{
            hqlHelper.orDerBy("replyCount",reverse);
        }
        PageBean pageBean=topicService.getPageBean(hqlHelper,getCurrentPage());
        getValueStack().set("show",pageBean.getRecordList());
        getValueStack().push(pageBean);
        return "List";
    }

    /**
     * 根据主题id查询评论
     * @return
     */
    public String Reply(){
        //根据id查询主题
        Topic topic=topicService.getById(model.getId());
        getValueStack().set("topic",topic);
        ServletActionContext.getRequest().getSession().setAttribute("id",model.getId());
        HQLHelper hqlHelper=new HQLHelper(Reply.class);
        hqlHelper.addWhere("topic=?",model);
        hqlHelper.orDerBy("postTime",true);
        PageBean pb=replyService.getPageBean(hqlHelper,getCurrentPage());
        getValueStack().push(pb);
        return "reply";
    }

    /**
     * 跳转添加页面
     * @return
     */
    public String addUi(){
        Forum forum=forumService.getById(model.getId());
        getValueStack().push(forum);
        return "addUi";
    }

    /**
     * 保存数据
     * @return
     */
    public String add(){

        //获取ip
        String ip=ServletActionContext.getRequest().getRemoteAddr();
        model.setIpAddress(ip);

        //获取时间
        Date date=new Date();
        model.setPostTime(date);

        //获取用户
        User user= (User) ServletActionContext.getRequest().getSession().getAttribute("user");
        model.setAuthor(user);

        //获取再那个板块发的主题
        Long id = model.getId();
        Forum forum=forumService.getById(id);

        //设置保存板块
        model.setForum(forum);

        //保存数据
        topicService.Save(model);

        getValueStack().set("show",topicList(id));
        getValueStack().set("forumName",forum.getName());
        return "List";
    }

    /**
     * 修改帖子类型
     * @return
     */
    public String setType(){
        Topic topic=topicService.getById(model.getId());
        topic.setType(model.getType());
        Forum forum=topic.getForum();
        topicService.Update(topic);
        getValueStack().set("show",topicList(forum.getId()));
        return "List";
    }
    //跳转修改帖子页面
    public String MoveUi(){
         Long id=   model.getId();
        Topic topic=topicService.getById(id);
        List<Forum> forum=forumService.findAll();
        getValueStack().set("forums",forum);
        getValueStack().push(topic);
        return "moveUi";
    }
    public String move(){
        Topic topic=topicService.getById(model.getId());
        Forum forum=forumService.getById(forumId);
        topic.setForum(forum);
        topicService.Update(topic);
        return "List";
    }
    /**
     * 查询数据
     */
    public List<Topic> topicList(Long id){
        List<Topic> list=topicService.Show(id);
        return list;
    }
}
